package our.pack;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class SaveTrackActivity extends Activity {
	
	private Button save, discard;
	private EditText titel;
	private Track track;
	private ListView listView;
	private static final String TAG = "SaveTrackActivity";
	
	private TracksDataSource trackDatasource;
	private PointsDataSource pointsDatasource;
	
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.savetrack);
		Intent i = getIntent();
		Bundle b = i.getExtras();
		
		track = (Track) b.get("Track");
		
		
		//db
		trackDatasource = new TracksDataSource(this);
		trackDatasource.open();
		pointsDatasource = new PointsDataSource(this);
		pointsDatasource.open();
		
		//UI
		save = (Button) findViewById(R.id.trackSave);
		discard = (Button) findViewById(R.id.trackDiscard);
		titel = (EditText) findViewById(R.id.trackTitel);
		
		//onClickListeners
		discard.setOnClickListener(new OnClickListener(){
			public void onClick(View v){
				
				trackDatasource.deleteTrack(track);
				
				finish();
			}
		});
		
		save.setOnClickListener(new OnClickListener(){
			public void onClick(View v){
				
				Log.d(TAG, "trackID" + track.getID());
				Log.d(TAG, "tracknaam: " + titel.getText());
				
				if(titel.getText().toString().length() == 0 ) {
					showToast("length of name must be larger then 0");
				} 
				else {
					track.setTrackName(titel.getText().toString());
					trackDatasource.renameTrack(track, titel.getText().toString());
					
					trackDatasource.close();
					pointsDatasource.close();
					finish();
				}
				
				
			}
		});
		
	}
	
	public void setListView(ListView listview){
		this.listView = listview;
	}
	
    public void showToast(CharSequence text) {
		Context context = super.getBaseContext().getApplicationContext();
		int duration = Toast.LENGTH_LONG;

		Toast toast = Toast.makeText(context, text, duration);
		toast.show();
	}

}
